﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Power.Common;
using Power.BLL;
using Power.Manager;
using JN.Utility;
using System.IO.Ports;

namespace Power.MeterControl
{
    public partial class MeterManual_PH : DevExpress.XtraEditors.XtraForm
    {
        #region property
        Com_infoBLL comBLL = new Com_infoBLL();
        SystemStatus sysStatus = SystemStatus.Instance;
        private SerialPort meterPort;
        RealData_PMH630 gData = new RealData_PMH630();
        #endregion
        public MeterManual_PH()
        {
            InitializeComponent();
        }

        private void MeterManual_Load(object sender, EventArgs e)
        {
            //load data
            LoadComPort();
        }
        private void LoadComPort()
        {
            try
            {
                DataTable dtData = comBLL.GetList(" station_id='"+sysStatus.Station_id+"' ").Tables[0];
                for (int i = 0; i < dtData.Rows.Count; i++)
                {
                    cmbPort.Properties.Items.Add(dtData.Rows[i]["com_port"].ToString());
                }
                cmbPort.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
                Common.Notify.NotifyMessage.Show(ex);
            }
        }
        private void LoadComProperty()
        {
            try
            {
                //get data
                Model.Com_info mPort = comBLL.GetModel(cmbPort.Text, sysStatus.Station_id);
                if (mPort != null)
                {
                    //if (meterPort != null)
                    //{
                    //    meterPort.Close();
                    //}
                    meterPort = new SerialPort();
                    meterPort.PortName = cmbPort.Text;
                    meterPort.BaudRate = int.Parse(mPort.baud_rate);
                    meterPort.DataBits = int.Parse(mPort.data_bit);
                    switch (mPort.odd.ToString().ToUpper())
                    {
                        case "NONE":
                            meterPort.Parity = Parity.None;
                            break;
                        case "ODD":
                            meterPort.Parity = Parity.Odd;
                            break;
                        case "EVEN":
                            meterPort.Parity = Parity.Even;
                            break;
                    }
                    switch (mPort.stop_bit.ToString())
                    {
                        case "1":
                            meterPort.StopBits = StopBits.One;
                            break;
                        case "1.5":
                            meterPort.StopBits = StopBits.OnePointFive;
                            break;
                        case "2":
                            meterPort.StopBits = StopBits.Two;
                            break;
                    }
                    meterPort.ReadTimeout = 500;
                    meterPort.WriteTimeout = 500;
                    //meterPort.Open();   //open all port

                    //add node
                    cmbNode.Properties.Items.Clear();
                    for (int i = 0; i < mPort.node_count; i++)
                    {
                        cmbNode.Properties.Items.Add((i + 1).ToString("X"));
                    }
                    cmbNode.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
                Common.Notify.NotifyMessage.Show(ex);
            }
        }
        private void MergerCmd()
        {
            try
            {
                if (cmbNode.Text == "") return;
                StringBuilder cmd = new StringBuilder();
                cmd.Append(cmbNode.Text.PadLeft(2,'0')+"03");
                if (txtAddr.Text == "") return;
                cmd.Append(txtAddr.Text.PadLeft(4,'0'));
                if (txtLength.Text == "") return;
                cmd.Append(txtLength.Text.PadLeft(4, '0'));
                txtCmd.Text = cmd.ToString();
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
                Common.Notify.NotifyMessage.Show(ex);
            }
        }
        private void cmbPort_TextChanged(object sender, EventArgs e)
        {
            //load com
            LoadComProperty();
            MergerCmd();
        }

        private void txtAddr_TextChanged(object sender, EventArgs e)
        {
            MergerCmd();
        }

        private void txtLength_TextChanged(object sender, EventArgs e)
        {
            MergerCmd();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            //send data
            if (txtCmd.Text == "")
            {
                Common.Notify.NotifyMessage.Show("警告", "指令不能为空");
                return;
            }
            if (txtCmd.Text.Length <12)
            {
                Common.Notify.NotifyMessage.Show("警告", "指令格式错误，请确认");
                return;
            }
            if (!meterPort.IsOpen)
            {
                Common.Notify.NotifyMessage.Show("警告", "请先打开端口!!");
                return;
            }
            string revMsg = "";
            MeterCommon.Instance.Write(meterPort, txtCmd.Text, ref revMsg);
            txtRetValue.Text = revMsg;
        }

        private void btnAnalys_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtRetValue.Text == "")
                {
                    Common.Notify.NotifyMessage.Show("警告", "返回结果不能为空");
                    return;
                }
                if (txtRetValue.Text.Length < 12)
                {
                    Common.Notify.NotifyMessage.Show("警告", "返回结果格式错误，请确认");
                    return;
                }
                //get meter id
                txtmeterid.Text = Manager.Manager.MeterEntity.GetMeterID(cmbPort.Text,cmbNode.Text);

                //get real data
                object dat1 = Manager.Manager.MeterEntity.AnalyseData(txtRetValue.Text, "1");

                //change data
                RealData_PMH630 data = (RealData_PMH630)dat1;
                data.meter_id = txtmeterid.Text;
                gData = data;
                #region 赋值
                txtexpa.Text = data.Exp_A_H.ToString();
                txtexpr.Text = data.Exp_R_H.ToString();
                txtf.Text = data.F.ToString();
                txti1.Text = data.I_a.ToString();
                txti2.Text = data.I_b.ToString();
                txti3.Text = data.I_c.ToString();
                txtiavg.Text = data.I_avg.ToString();
                txtid.Text = data.I_d.ToString();
                txtimpa.Text = data.Imp_A_H.ToString();
                txtimpr.Text = data.Imp_R_H.ToString();
                txtp1.Text = data.P_a.ToString();
                txtp2.Text = data.P_b.ToString();
                txtp3.Text = data.P_c.ToString();
                txtpf.Text = data.PF.ToString();
                txtpf1.Text = data.PF_a.ToString();
                txtpf2.Text = data.PF_b.ToString();
                txtpf3.Text = data.PF_c.ToString();
                txtpt.Text = data.P_t.ToString();
                txtpt1.Text = data.Pt1_.ToString();
                txtpt2.Text = data.Pt2.ToString();
                txtq1.Text = data.Q_a.ToString();
                txtq2.Text = data.Q_b.ToString();
                txtq3.Text = data.Q_c.ToString();
                txtqt.Text = data.Q_t.ToString();
                txts1.Text = data.S_a.ToString();
                txts2.Text = data.S_b.ToString();
                txts3.Text = data.S_c.ToString();
                txtst.Text = data.S_t.ToString();
                txtswith.Text = data.Swith_Data.ToString();
                txtu1.Text = data.U_ab.ToString();
                txtu2.Text = data.U_bc.ToString();
                txtu3.Text = data.U_ca.ToString();
                txtuavg.Text = data.U_avg.ToString();
                txtv1.Text = data.V_a.ToString();
                txtv2.Text = data.V_b.ToString();
                txtv3.Text = data.V_c.ToString();
                txtvavg.Text = data.V_avg.ToString();
                txtct.Text = data.Ct1.ToString();
                #endregion
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
                Common.Notify.NotifyMessage.Show(ex);
            }
        }

        private void cmbNode_TextChanged(object sender, EventArgs e)
        {
            MergerCmd();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Manager.Manager.MeterEntity.SaveReal2DB((object)gData);
        }

        private void btnOpenPort_Click(object sender, EventArgs e)
        {
            if (meterPort != null && !meterPort.IsOpen)
            {
                meterPort.Open();
            }
        }

        private void btnClosePort_Click(object sender, EventArgs e)
        {
            if (meterPort != null)
            {
                meterPort.Close();
            }
        }
        
    }
}